from odoo import fields, models, api
from odoo.exceptions import UserError


class RPCourier(models.Model):
    _inherit = "rp.courier"

    third_name = fields.Char(string='第三方承运商名称', copy=False, readonly=True, tracking=True)
    third_id = fields.Char(string='第三方承运商编号', copy=False, readonly=True, tracking=True)
    third_type = fields.Selection(string="接口提供方", selection=[])
    default_price_adjust_rule = fields.Many2one("rp.third.price.adjust.rule", "默认调价规则")


class RPCourierMailRouteInherit(models.Model):
    _inherit = 'rp.courier.mail.route'

    is_third_server = fields.Boolean(string='第三方服务', default=False)
    rp_third_route_id = fields.Many2one("rp.third.route.collect", "第三方邮路")
    third_server_id = fields.Char(string='第三方服务编号', related="rp_third_route_id.route_id", copy=False, readonly=False, tracking=True)
    third_route_name = fields.Char(string='第三方服务名称', related="rp_third_route_id.name", copy=False, readonly=False, tracking=True)
    third_name = fields.Char(related="rp_third_route_id.courier_id.name")
    third_type = fields.Selection(related="rp_third_route_id.third_type")
    # shippypro_id = fields.Char(string='ShipPyPro索引')
    volume_weight_id = fields.Many2one(required=False)

    price_adjust_rule = fields.Many2one("rp.third.price.adjust.rule", "调价规则")

    # def _get_real_rule(self):
    #     if not self.price_adjust_rule:
    #         return self.courier_id.default_price_adjust_rule
    #     else:
    #         return self.price_adjust_rule


class RPShipperMailRoute(models.Model):
    _inherit = "rp.shipper.mail.route"
    _sql_constraints = [
        (
            "unique_name",
            "UNIQUE(name,shipper_id,courier_mail_route_id)",
            "同一个托运商，邮路名称必须唯一",
        ),
        # ("unique_code", "UNIQUE(code,shipper_id)", "同一个托运商，邮路编码必须唯一"),
    ]
